/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.registration;

import components.TabPanel;
import components.utils.ComponentFactory;
import components.utils.CustomVerifier;
import control.Application;
import entity.Employee;
import java.util.Arrays;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class PEmployeeRegistration extends TabPanel {

    /**
     * Creates new form PEmployeeRegistration
     */
    public PEmployeeRegistration() {
        initComponents();
        TFName.requestFocusInWindow();
        ComponentFactory.attachVerifier("Por favor ingrese el nombre del empleado.", TFName);
        ComponentFactory.attachVerifier("Por favor ingrese el número de documento del empleado.", TFIdNum);
        ComponentFactory.attachVerifier("Por favor ingrese el tipo de documento de identidad.", TFOther);
        ComponentFactory.attachVerifier("Por favor ingrese el nombre de usuario.", TFUsername);
        ComponentFactory.attachVerifier("Por favor ingrese la contraseña de este empleado.", PFPassword);
        ComponentFactory.attachVerifier("Por favor ingrese la confirmación de la contraseña del empleado.", PFPasswordConfirmation);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        TFName = new javax.swing.JTextField();
        TFIdNum = new javax.swing.JTextField();
        TFOther = new javax.swing.JTextField();
        TFUsername = new javax.swing.JTextField();
        PFPassword = new javax.swing.JPasswordField();
        PFPasswordConfirmation = new javax.swing.JPasswordField();
        BRegister = new javax.swing.JButton();
        BCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        RBCC = new javax.swing.JRadioButton();
        RBCE = new javax.swing.JRadioButton();
        RBPassport = new javax.swing.JRadioButton();
        RBOther = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        TFOther.setEnabled(false);

        BRegister.setText("Registrar Empleado");
        BRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BRegisterActionPerformed(evt);
            }
        });

        BCancel.setText("Cancelar");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre del Empleado:");

        jLabel2.setText("Número de Documento:");

        buttonGroup1.add(RBCC);
        RBCC.setSelected(true);
        RBCC.setText("CC");

        buttonGroup1.add(RBCE);
        RBCE.setText("CE");

        buttonGroup1.add(RBPassport);
        RBPassport.setText("Pasaporte");

        buttonGroup1.add(RBOther);
        RBOther.setText("Otro:");
        RBOther.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBOtherActionPerformed(evt);
            }
        });

        jLabel3.setText("Nombre de Usuario:");

        jLabel4.setText("Contraseña:");

        jLabel5.setText("Confirmar Contraseña:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TFUsername)
                    .addComponent(TFName)
                    .addComponent(PFPassword)
                    .addComponent(PFPasswordConfirmation)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(TFIdNum, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(RBCC)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(RBCE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(RBPassport)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(RBOther)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(TFOther, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BRegister)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BCancel)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFIdNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RBCC)
                    .addComponent(RBCE)
                    .addComponent(RBPassport)
                    .addComponent(RBOther)
                    .addComponent(TFOther, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PFPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PFPasswordConfirmation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BRegister)
                    .addComponent(BCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void RBOtherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBOtherActionPerformed
        if (RBOther.isSelected()) {
            TFOther.setEnabled(true);
        } else {
            TFOther.setEnabled(false);
            TFOther.setText(null);
        }
    }//GEN-LAST:event_RBOtherActionPerformed

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BCancelActionPerformed

    private void BRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BRegisterActionPerformed
        Employee newEmployee = new Employee();
        CustomVerifier currentVerifier;
        
        currentVerifier = (CustomVerifier) TFName.getInputVerifier();
        if (currentVerifier.isValid()) {
            newEmployee.setName(TFName.getText());
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) TFIdNum.getInputVerifier();
        if (currentVerifier.isValid()) {
            newEmployee.setIdNum(TFIdNum.getText());
        } else {
            return;
        }
        
        if (RBCC.isSelected()) {
            newEmployee.setIdType("CC");
        } else if (RBCE.isSelected()) {
            newEmployee.setIdType("CE");
        } else if (RBPassport.isSelected()) {
            newEmployee.setIdType("PASS");
        } else {
            currentVerifier = (CustomVerifier) TFOther.getInputVerifier();
            if (currentVerifier.isValid()) {
                newEmployee.setIdType(TFOther.getText());
            } else {
                return;
            }
        }
        
        currentVerifier = (CustomVerifier) TFUsername.getInputVerifier();
        if (currentVerifier.isValid()) {
            newEmployee.setLogin(TFUsername.getText());
        } else {
            return;
        }
        
        char[] password1;
        char[] password2;
        currentVerifier = (CustomVerifier) PFPassword.getInputVerifier();
        if (currentVerifier.isValid()) {
            password1 = PFPassword.getPassword();
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) PFPasswordConfirmation.getInputVerifier();
        if (currentVerifier.isValid()) {
            password2 = PFPasswordConfirmation.getPassword();
        } else {
            return;
        }
        
        //TODO verify passwords
        
        newEmployee.setPassword(String.valueOf(password1));
        Arrays.fill(password1, (char) 0);
        Arrays.fill(password2, (char) 0);
        Application.EMPLOYEE_MANAGER.addObject(newEmployee);
        JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                "El empleado se ha registrado correctamente.",
                "Información", JOptionPane.INFORMATION_MESSAGE);
        buttonTabComponent.closeTab();
        
        //TODO persist
    }//GEN-LAST:event_BRegisterActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancel;
    private javax.swing.JButton BRegister;
    private javax.swing.JPasswordField PFPassword;
    private javax.swing.JPasswordField PFPasswordConfirmation;
    private javax.swing.JRadioButton RBCC;
    private javax.swing.JRadioButton RBCE;
    private javax.swing.JRadioButton RBOther;
    private javax.swing.JRadioButton RBPassport;
    private javax.swing.JTextField TFIdNum;
    private javax.swing.JTextField TFName;
    private javax.swing.JTextField TFOther;
    private javax.swing.JTextField TFUsername;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}
